package com.vogelsk.archangelsk.creatures;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.vogelsk.archangelsk.creatures.util.Command;
import com.vogelsk.archangelsk.creatures.util.Coordinate;
import com.vogelsk.archangelsk.creatures.util.Direction;

public class Tank extends GenericCreature {

	private static final int DEFAULT_TANK_SIZE = 1;
	private static final int DEFAULT_TANK_WIDTH = 20;
	private static final int DEFAULT_TANK_LENGTH = 25;
	private static final int DEFAULT_TANK_SPEED = 20;
	public static final String TEAM_A = "player";
	public static final String TEAM_B = "enemy";
	private boolean shotFired;
	private String team;
	private List<Coordinate> tracks;
	private Command order;
	Random randy = new Random();

	public Tank(int size, int x, int y, int speed, Direction direction) {
		super(size, x, y, speed, direction);
	}

	public Tank(int x, int y, Direction direction, String team) {
		super(DEFAULT_TANK_SIZE, x, y, DEFAULT_TANK_SPEED, direction);
		this.setTeam(team);
		this.setTracks(new ArrayList<Coordinate>());
		this.setOrder(Command.MANOUVER);
		super.setDestructable(true);
	}

	public void rotate(Direction direction) {
		if (!isDestroyed()){
			if (direction == null) {
				setDirection(Direction.values()[randy.nextInt(4)]);
				return;
			}

			if ((direction.equals(Direction.RIGHT) && !getDirection().equals(
					Direction.LEFT))
					|| (direction.equals(Direction.LEFT) && !getDirection().equals(
							Direction.RIGHT))
					|| (direction.equals(Direction.DOWN) && !getDirection().equals(
							Direction.UP))
					|| (direction.equals(Direction.UP) && !getDirection().equals(
							Direction.DOWN))) {
				setDirection(direction);
			}
		}
	}

	public void revertMove() {
		if (!getTracks().isEmpty()) {
			Coordinate xy = getTracks().get(getTracks().size() - 1);
			setX(xy.getX());
			setY(xy.getY());
			getTracks().remove(xy);
		}

	}

	@Override
	public void move() {
		getTracks().add(new Coordinate(getX(), getY()));
		super.move();
	}

	public void aiMove(int steps) {
		for (int i = 0; i < steps; i++) {
			this.move();
		}
	}

	public void manuouver() {

		if (randy.nextBoolean()){
			rotate(null);
		aiMove(1);}
		else
			aiMove(1);

	}

	public Bullet fire() {
		if (!shotFired && !isDestroyed()) {
			shotFired = true;
			int x = getX();
			int y = getY();
			if (getDirection().equals(Direction.LEFT)){

				y += DEFAULT_TANK_WIDTH;	
			}
			if (getDirection().equals(Direction.RIGHT)){
				x += DEFAULT_TANK_LENGTH;
				y += DEFAULT_TANK_WIDTH;	
			}
			if (getDirection().equals(Direction.UP))
				x += DEFAULT_TANK_WIDTH;
			if (getDirection().equals(Direction.DOWN)) {
				x += DEFAULT_TANK_WIDTH;
				y += DEFAULT_TANK_LENGTH;
			}

			Bullet bullet = new Bullet(x, y, this);
			return bullet;
		} else
			return null;
	}

	public void setShotFired(boolean shotFired) {
		this.shotFired = shotFired;
	}

	public boolean isShotFired() {
		return shotFired;
	}

	public void setTeam(String team) {
		this.team = team;
	}

	public String getTeam() {
		return team;
	}

	public void setTracks(List<Coordinate> tracks) {
		this.tracks = tracks;
	}

	public List<Coordinate> getTracks() {
		return tracks;
	}

	public void setOrder(Command order) {
		this.order = order;
	}

	public Command getOrder() {
		return order;
	}
}
